# -*- coding: utf-8 -*-
from urllib2 import urlopen, URLError, HTTPError
import zipfile
#from io import StringIO
import csv
import os
from db_conn import *
import re
import traceback
import time
import threading
import Queue
import logging
import datetime

from time import localtime, strptime, strftime, mktime

from datetime import datetime, timedelta

import socket

timeout = 600
socket.setdefaulttimeout(timeout)

arquivo=100
debug=0

if not len(sys.argv) < 2:
    
    for v in sys.argv:
        if len(v.split("="))>1:
            param = v.split("=")[0].strip()
            valor = v.split("=")[1].strip()
            try:
                if param.lower().strip().find("url_play")>-1:
                    url_play = valor
                    #print "url_play: %s" % (url_play) 
                    if url_play.find(",")>-1:
                        url_play_array = url_play.split(",")
                        for url_print_arr in url_play_array:
                            print "url_snd: %s" % (url_print_arr)
                    else:
                        url_play_array = [url_play]
                        print "url_snd: %s" % (url_play_array)
                elif param.lower().strip().find("max_qtd_plays")>-1:
                    max_qtd_plays = valor
                    print "max_qtd_plays: %s" % (max_qtd_plays)
                elif param.lower().strip().find("debug")>-1:
                    debug = valor
                    print "debug: %s" % (debug)                                                      
                else:
                    eval('%s=%s' % (param,valor))   
                #print "Param: %s" % (param)
                #print "Valor: %s" % (valor)
            except:
                exceptionType, exceptionValue, exceptionTraceback = sys.exc_info()
                traceback.print_exception(exceptionType, exceptionValue, exceptionTraceback,
                                  limit=2, file=sys.stdout)  

else:
    print "try use: python %s url_play=value max_qtd_plays=value" % (sys.argv[0])
    print "Batch Job using default params!"

def timeStamped(fname, fmt='{fname}_%Y-%m-%d-%H-%M-%S.out'):
    return datetime.now().strftime(fmt).format(fname=fname)
   
try:      

    WORKERS = 5
    
    class Worker(threading.Thread):
    
        def __init__(self, queue):
            self.__queue = queue
            threading.Thread.__init__(self)
    
        def run(self):
            while 1:
                item = self.__queue.get()
                if item[0] is None:
                    break # reached end of queue
                
                #url = item[0]
                filename = item[1]
                logger = item[2]
                
                try:
                    start = time.time()  
                    #print "URL: %s" % row[-1]
                    
                    #dlfile(url,filename,logger)
                    #print "Tamanho: %s" % url.read().__sizeof__()
                            
                    z = zipfile.ZipFile(filename)
                    #z = zipfile.ZipInfo(filename)
                     
                    #lines_zip = 0
                    try:
                        cursor,conn = db_connect()
                        for line in z.open(z.namelist()[0]).readlines():
                            #print line
                            #logger.debug(line)
                            if line.find("Distancia")==-1:
                            #if lines_zip>0:
                                '''
                                insert_values = ""
                                for col in line.split("|"):
                                    insert_values += """'%s',""" % (re.escape(col))              
                                
                                insert_anatel="""
                                insert into anatel_real
                                values(NULL,%s'%s',NULL);
                                """ % (insert_values,filename)
                                #print insert_anatel
                                logger.debug(insert_anatel)
                                cursor.execute(insert_anatel)
                                '''
                                gmt_offset_brazil = -7
                                date_now = datetime.now() - timedelta(minutes=(gmt_offset_brazil)) #gmtoffset (brazil time)
                                #out_file_name = date_now.strftime("%d/%m/%Y %H:%M")
                                out_file_name = "cad_anatel" + date_now.strftime("-%d-%m-%Y") + ".txt"
                                with open(out_file_name, "a") as myfile:
                                    myfile.write(line)
                            #lines_zip += 1
                        db_disconnect(cursor,conn)
                    except:
                        logger.error(str(sys.exc_info()))
            
                        
                except IOError, e:
                    #print "Nao foi possivel baixar o zip!"
                    #print e
                    logger.error(e)
                except zipfile.error, e:
                    #print "Erro ao descompactar, nao eh zip!"
                    #print e
                    logger.error(e)
                
                task_message = "task ", item[1], " finished! (Tempo Gasto: %s)" % ((time.time() - start))
                print task_message
                logger.info(task_message)
    
    #######LOGGING###################
    LOG_FILENAME = timeStamped('log')
    # create logger
    logger = logging.getLogger("Laguardia")
    logger.setLevel(logging.INFO)
    # create console handler and set level to debug
    ch = logging.StreamHandler()
    #ch = logger.handlers.RotatingFileHandler(
    #          LOG_FILENAME)
    ch.setLevel(logging.DEBUG)
    # create formatter
    formatter = logging.Formatter(
        "%(asctime)s - %(name)s - %(levelname)s - %(message)s")
    # add formatter to ch
    ch.setFormatter(formatter)
    # add ch to logger
    logger.addHandler(ch)
    
    # "application" code
    #logger.debug("debug message")
    #logger.info("info message")
    #logger.warn("warn message")
    #logger.error("error message")
    #logger.critical("critical message")

    queue = Queue.Queue(0)
    
    estados = ['AC','AL','AP','AM','BA','CE','DF','ES','GO','MA','MT','MS','MG','PA','PB','PR','PE','PI','RJ','RN','RS','RO','RR','SC','SP','SE','TO']    
    freq = ['414:448','1427:1517','3800:4200','4400:5000','5925:6425','6430:7110','7425:7725','7725:8275','8275:8500','10700:11700','12200:13250','14500:15350','17700:19700','21300:23600','37000:39500']
    DIVISAO_CONST=100
    
    for i in range(WORKERS):
        Worker(queue).start() # start a worker
    
    count_files = 0
    for uf in estados:
        for fr in freq:
            fr_inicial = fr.split(':')[0]
            fr_final = fr.split(':')[1]
            
            range_int = ((int(fr_final)-int(fr_inicial))/DIVISAO_CONST)+1
            print "RangeInt: %s" % (range_int)
            for i in range(0,range_int):
                print "i = %s" % (i)
                fr_inicial_loop = int(fr_inicial)+(i*DIVISAO_CONST)
                if int(fr_final) < int(fr_inicial)+((i+1)*DIVISAO_CONST):
                    fr_final_loop = int(fr_final)
                else:
                    fr_final_loop = int(fr_inicial)+((i+1)*DIVISAO_CONST)
                    
                if fr_inicial_loop==fr_final_loop:
                    break
                
                count_files+=1             
                print "Inicial:%s  Final:%s" % (fr_inicial_loop,fr_final_loop)
                template_url='http://sistemas.anatel.gov.br/stel/Consultas/RecuperacaoFrequencias/tela.asp?SISQSmodulo=9896&IndFiltro=u&IndApresentacao=i&pMedTransmissaoInicial=%s&pMedRecepcaoInicial=%s&pIdtUnidadeTransmissao=2&pIdtUnidadeRecepcao=2&SiglaUF=%s&IndGrupoEstacao=r&botaoFlatDownloadTXT="true"&acao=t' % (fr_inicial_loop,fr_final_loop,uf)
                #print template_url
                filename = uf+'_'+str(fr_inicial_loop)+'_'+str(fr_final_loop)+'.zip'
                logger.info(filename)
                url=template_url
                queue.put([url,filename,logger])
    
    for i in range(WORKERS):
        queue.put([None,None,None]) # add end-of-queue markers
    
    
except KeyboardInterrupt:
    print('Interrompendo!!')
    logger.critical('Interrompendo!!')
    sys.exit()
except:
    exceptionType, exceptionValue, exceptionTraceback = sys.exc_info()
    traceback.print_exception(exceptionType, exceptionValue, exceptionTraceback,
                      limit=2, file=sys.stdout)
    logger.error(str(exceptionValue))